package com.pc.config;

/**
 * @author 李林雨
 * @createTime 2024/1/19 10:09
 * @description
 */

import com.pc.Server.MQTTServer;
import com.pc.Server.RedisService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

/**
 * 主要用来接收和处理订阅主题的消息
 */

public class PushCallback implements MqttCallback {
    RedisService redisService;
    private static final Logger LOGGER = LoggerFactory.getLogger(PushCallback.class);
    //引入redis
//    public static   HashMap<String, String> mqttHashMap =  new HashMap<>();
    private MQTTServer mqttServer;

    public PushCallback(MQTTServer mqttServer) {
        this.mqttServer = mqttServer;
    }


    public void connectionLost(Throwable cause) {
        // 连接丢失后，一般在这里面进行重连
        LOGGER.info("---------------------连接断开，可以做重连");
        mqttServer.subsribeConnect();

        while (true){
            try {
                //如果没有发生异常说明连接成功，如果发生异常，则死循环
                Thread.sleep(1000);
                break;
            }catch (Exception e){
                continue;
            }
        }

    }

    /**
     * 发送消息，消息到达后处理方法
     * @param token
     */
    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());
    }

    /**
     * 接收所订阅的主题的消息并处理
     * @param topic
     * @param message
     */
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        // subscribe后得到的消息会执行到这里面
        String result = new String(message.getPayload(),"UTF-8");

//        mqttHashMap.put(topic,result);


        System.out.println("接收消息主题 : " + topic);
        System.out.println("接收消息Qos : " + message.getQos());
        System.out.println("接收消息内容 : " + result);
        //这里可以针对收到的消息做处理，比如持久化,储存到redis中
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://localhost:8020/hello2/"+topic+"/"+result).build();
        try {
            Response response = client.newCall(request).execute();

            if (response.isSuccessful()) {
                String responseBody = Objects.requireNonNull(response.body()).string();
                System.out.println(responseBody);
            } else {
                System.out.println("请求失败，错误码：" + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}